package com.springbootxuexi.controller;

import com.springbootxuexi.domain.ErrorCodes;
import com.springbootxuexi.domain.JsonResponse;
import com.springbootxuexi.domain.Student;
import com.springbootxuexi.service.StudentService;
import org.hibernate.loader.plan.exec.process.internal.EntityReturnReader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class StudentController {

    @Autowired
    private StudentService studentService;

    @CrossOrigin
    @PostMapping("/signup.php")
    public JsonResponse<Map<String,String>> save(@RequestBody @Valid Student student, BindingResult result)
    {
        //TODO something to do here.
        Map<String,String> response = new HashMap<String,String>();
        if(result.hasErrors()) {
            for (ObjectError error :  result.getAllErrors()) {
                response.put("detail",error.getDefaultMessage());
            }
            return new JsonResponse<Map<String,String>>(ErrorCodes.CODE_PARAM_ERR,response);
        }else{
            if(this.studentService.save(student)!=-1){
                response.put("detail","注册成功");
                return new JsonResponse<Map<String,String>>(ErrorCodes.CODE_OK,response);
            }
            else{
                response.put("detail","用户名已存在");
                return new JsonResponse<Map<String,String>>(ErrorCodes.ODE_USER_ALREADY_EXIST,response);
            }
        }
    }

    @CrossOrigin
    @PostMapping("/update.php")
    public JsonResponse<Map<String,String>> update(@RequestBody @Valid Student student, BindingResult result)
    {
        //TODO something to do here.
        Map<String,String> response = new HashMap<String,String>();
        if(result.hasErrors()) {
            for (ObjectError error :  result.getAllErrors()) {
                response.put("detail",error.getDefaultMessage());
            }
            return new JsonResponse<Map<String,String>>(ErrorCodes.CODE_PARAM_ERR,response);
        }else{
            if(this.studentService.update(student)!=-1){
                response.put("detail","注册成功");
                return new JsonResponse<Map<String,String>>(ErrorCodes.CODE_OK,response);
            }
            else{
                response.put("detail","用户名不存在");
                return new JsonResponse<Map<String,String>>(ErrorCodes.ODE_USER_ALREADY_EXIST,response);
            }
        }
    }


    @CrossOrigin
    @PostMapping(value = "/login.php")
    public JsonResponse<Map<String,String>> login(@RequestBody @Valid Student student, BindingResult result){
        System.out.println(student.toString());
        Map<String,String> response = new HashMap<String,String>();
        if(result.hasErrors()) {
            for (ObjectError error :  result.getAllErrors()) {
//                System.out.println(error.getCode() + "---" + error.getArguments() + "---" + error.getDefaultMessage());
                response.put("detail",error.getDefaultMessage());
            }
            return new JsonResponse<Map<String,String>>(ErrorCodes.CODE_PARAM_ERR,response);
        }else{

            JsonResponse<Map<String,String>> ret = null;
            int res = this.studentService.login(student);
            switch(res)
            {
                case 0:{
                    response.put("detail","查询失败");
                    ret = new JsonResponse<Map<String,String>>(ErrorCodes.CODE_PARAM_ERR,response);
                };break;
                case -1:{
                    response.put("detail","用户名不存在");
                    ret = new JsonResponse<Map<String,String>>(ErrorCodes.CODE_USER_NOT_EXIST,response);
                };break;
                case -2:{
                    response.put("detail","密码错误");
                    ret = new JsonResponse<Map<String,String>>(ErrorCodes.CODE_USER_NOT_EXIST,response);
                };break;
                default:{
                    response.put("detail",String.valueOf(res));
                    ret = new JsonResponse<Map<String,String>>(ErrorCodes.CODE_OK,response);
                }
            }
                return ret;
    }
    }

    @CrossOrigin
    @GetMapping("/listall.php")
    public JsonResponse<List<Student>> signup(){
        return new JsonResponse<List<Student>>(ErrorCodes.CODE_OK,this.studentService.findall());
    }


    @CrossOrigin
    @GetMapping("/findone.php")
    public JsonResponse<Student> findOne(Integer id){
        Student student = this.studentService.findOne(id);
        JsonResponse<Student> ret = null;
        if(student==null){
            ret = new JsonResponse<Student>(ErrorCodes.CODE_USER_NOT_EXIST,null);
        }
        else
        {
            ret =  new JsonResponse<Student>(ErrorCodes.CODE_OK,student);
        }
        return ret;
    }

    @CrossOrigin
    @GetMapping("delete.php")
    public JsonResponse<Map<String,String>> deleteById(Integer id)
    {
        JsonResponse<Map<String,String>> ret = null;
        Map<String,String> mapRet = this.studentService.delete(id);
        if(mapRet.get("code").equals("0")){
            ret = new JsonResponse<Map<String,String>>(ErrorCodes.CODE_OK,mapRet);
        }else{
            ret = new JsonResponse<Map<String,String>>(ErrorCodes.CODE_USER_NOT_EXIST,mapRet);
        }
        return ret;
    }

    @CrossOrigin
    @GetMapping("count.php")
    public JsonResponse<Map<String,String>> count(){
        Map<String,String> ret = new HashMap<>();
        ret.put("count",String.valueOf(this.studentService.count()));
        return new JsonResponse<Map<String,String>>(ErrorCodes.CODE_OK,ret);
    }
}
